package com.interactiveyang.meet;

import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.util.Log;

import com.interactiveyang.meet.C;

public class BackendRequest {
	String result = "";
	
	public String responseJson;
	
	public BackendRequest(){
	}
	
	public boolean checkUserLogin(String usr, String pwd){
		
		ArrayList<NameValuePair> nameVaulePair = new ArrayList<NameValuePair>();
		nameVaulePair.add(new BasicNameValuePair("username", usr));
		nameVaulePair.add(new BasicNameValuePair("password", pwd));
		
		try {
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost post = new HttpPost(C.MeetBack.USER_LOGIN);
			
			post.setEntity(new UrlEncodedFormEntity(nameVaulePair));
			HttpResponse response = httpClient.execute(post);
			
			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				String isUserValid = EntityUtils.toString(response.getEntity());
				
				if(isUserValid.equals("user valid")){
					return true;
				}
				else{
					return false;
				}
				
			}
			else{
				return false;
			}
			
		} 
		catch (Exception e) {
			return false;
		}
	}
	
	public String getOtherUserInfo(String username){
		try{
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet get = new HttpGet(C.MeetBack.GET_USER_INFO + "?username=" + username);
			
			HttpResponse response = httpClient.execute(get);
			
			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				responseJson = EntityUtils.toString(response.getEntity());
				return responseJson;
			}
			else{
				return "Error when try to get data from MYSQL";
			}
		}
		catch(Exception e){
			Log.e("log_tag", "Error in http connection "+e.toString());
			return "Error when try to get data from MYSQL";
		}
	}
	
	public boolean saveUserCurrentLocation(double lat, double lan, String username){
		
		ArrayList<NameValuePair> nameVaulePair = new ArrayList<NameValuePair>();
		nameVaulePair.add(new BasicNameValuePair("username", username));
		nameVaulePair.add(new BasicNameValuePair("latitude", Double.toString(lat)));
		nameVaulePair.add(new BasicNameValuePair("langitude", Double.toString(lan)));
		
		try {
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost post = new HttpPost(C.MeetBack.SAVE_USER_CURRENT_LOCATION);
			
			post.setEntity(new UrlEncodedFormEntity(nameVaulePair));
			HttpResponse response = httpClient.execute(post);
			
			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				return true;
			}
			else{
				return false;
			}
		} 
		catch (Exception e) {
			return false;
		}
	}
	
	public boolean publishShoot(String shoutContents, String username){
		ArrayList<NameValuePair> nameVaulePair = new ArrayList<NameValuePair>();
		nameVaulePair.add(new BasicNameValuePair("username", username));
		nameVaulePair.add(new BasicNameValuePair("shoutContents", shoutContents));
		
		if(meetPost(nameVaulePair, C.MeetBack.POST_SHOUT))
			return true;
		else
			return false;
	}
	
	public String getUserShout(String username){
		try{
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet get = new HttpGet(C.MeetBack.GET_SHOUT + "?username=" + username);
			
			HttpResponse response = httpClient.execute(get);
			
			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				responseJson = EntityUtils.toString(response.getEntity());
				return responseJson;
			}
			else{
				return "Error when try to get data from MYSQL";
			}
		}
		catch(Exception e){
			Log.e("log_tag", "Error in http connection "+e.toString());
			return "Error when try to get data from MYSQL";
		}
	}
	
	private boolean meetPost(ArrayList<NameValuePair> nvp, String url){
		try {
			HttpClient httpClient = new DefaultHttpClient();
			HttpPost post = new HttpPost(url);
			
			post.setEntity(new UrlEncodedFormEntity(nvp));
			HttpResponse response = httpClient.execute(post);
			
			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				return true;
			}
			else{
				return false;
			}
		} 
		catch (Exception e) {
			return false;
		}
	}
	
//	private boolean meetGet(){
//		
//	}
}






